package com.qc31.comlib.choose.car

import autodispose2.autoDispose
import com.qc31.baselib.auto.AutoDisposeViewModel
import com.qc31.comlib.R
import com.qc31.comlib.entity.ChooseTeamCar
import com.qc31.comlib.net.ServiceHelper
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.subjects.BehaviorSubject

/**
 *@Author Jasper
 *@Time   2020/7/23
 *@Desc
 */
class ChooseCarViewModel : AutoDisposeViewModel() {
    private var pids = mutableListOf("0")
    private var names = mutableListOf<String>()
    var isVideo = false
    var rootName = ""
    private val carTeamList by lazy { mutableListOf<ChooseTeamCar>() }
    private val listObservable by lazy {
        BehaviorSubject.create<MutableList<ChooseTeamCar>>()
    }

    fun listObserver(): Observable<MutableList<ChooseTeamCar>> = listObservable.hide()
    val titleName: BehaviorSubject<String> by lazy { BehaviorSubject.create<String>() }

    fun init() {
        names.add(rootName)
        titleName.onNext(names[names.lastIndex])
    }

    fun getTeamCars() {
        toastLoading(true)
        ServiceHelper.getRepository().getTeamCars(pids[pids.lastIndex], isVideo).autoDispose(this)
            .subscribe({
                carTeamList.clear()
                for (team in it.teamList) {
                    if (isVideo) {
                        if (team.carNum != 0) carTeamList.add(
                            ChooseTeamCar(
                                pid = team.pid,
                                teamId = team.teamId,
                                carNum = team.carNum,
                                teamName = team.teamName
                            )
                        )
                    } else {
                        carTeamList.add(
                            ChooseTeamCar(
                                pid = team.pid,
                                teamId = team.teamId,
                                carNum = team.carNum,
                                teamName = team.teamName
                            )
                        )
                    }
                }
                for (car in it.carList) {
                    carTeamList.add(
                        ChooseTeamCar(
                            carId = car.carId,
                            carName = car.carName,
                            carPlate = car.carPlate,
                            iconLink = car.iconLink,
                            state = car.state,
                            accState = car.acc,
                            videos = car.videos,
                            teamId = car.teamId,
                            isVideo = !car.videos.isNullOrEmpty()
                        )
                    )
                }
                toastLoading(false)
                listObservable.onNext(carTeamList)
            }, error)
    }

    //上个车队
    fun getPreviousTeam() {
        if (pids.size <= 1) {
            toast( R.string.desc_root_team)
        } else {
            pids.removeAt(pids.lastIndex)
            names.removeAt(names.lastIndex)
            getTeamCars()
        }
        titleName.onNext(names[names.lastIndex])
    }

    //home
    fun getHomeTeam() {
        if (pids.size <= 1) {
            toast(R.string.desc_root_team_)
        } else {
            pids.clear()
            pids.add("0")
            names.add(rootName)
            getTeamCars()
        }
        titleName.onNext(names[names.lastIndex])
    }

    //下个车队
    fun getNextTeam(pid: String, teamName: String) {
        pids.add(pid)
        names.add(teamName)
        getTeamCars()
        titleName.onNext(names[names.lastIndex])
    }
}
